/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework Class Library
 * Version 2.00
 * 
 * Cryptographic Result Class
 *      Represents the result of the execution of a cryptographic operation.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.IO;

namespace Adaptive.Foundation.Security
{
    /// <summary>
    /// Represents the result of the execution of a cryptographic operation.
    /// </summary>
    public class CryptographicResult : DisposableObjectBase
    {
        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// Type of result.
        /// </summary>
        public CryptographicResultType _resultType = CryptographicResultType.Success;
        /// <summary>
        /// Overall Success or failure flag.
        /// </summary>
        public bool _success;
        /// <summary>
        /// Result of last operation, if successful.
        /// </summary>
        public Stream _resultData;
        /// <summary>
        /// Last exception that was encountered.
        /// </summary>
        public Exception _lastException;
        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public CryptographicResult()
            : base()
        {
        }
        /// <summary>
        /// Initializes a new instance of the class to a successful state for 
        /// the specified result stream data.
        /// </summary>
        /// <param name="resultData">
        /// The data resulting from the related operation.
        /// </param>
        public CryptographicResult(Stream resultData) : base()
        {
            _resultData = resultData;
            _success = true;
            _resultType = CryptographicResultType.Success;
            _lastException = null;

        }
        /// <summary>
        /// Initializes a new instance of the class to a failure state with  the specified
        ///  exception.
        /// </summary>
        /// <param name="lastException">
        /// The <see cref="Exception"/> instance that was caught.
        /// </param>
        public CryptographicResult(Exception lastException)
            : base()
        {
            _resultType = CryptographicResultType.UnknownException;
            _resultData = null;
            _success = false;
            _lastException = lastException;
        }
        /// <summary>
        /// Initializes a new instance of the class to a failure state with the specified
        /// result type and  exception.
        /// </summary>
        /// <param name="resultType">
        /// A <see cref="CryptographicResultType"/> enumerated value indicating the type of 
        /// result.
        /// </param>
        /// <param name="lastException">
        /// The <see cref="Exception"/> instance that was caught.
        /// </param>
        public CryptographicResult(CryptographicResultType resultType, Exception lastException)
            : base()
        {
            _resultType = resultType;
            _resultData = null;
            _success = false;
            _lastException = lastException;
        }
        /// <summary>
        /// Initializes a new instance of the class to the specified values.
        /// </summary>
        /// <param name="success">
        /// A value indicating whether the operation was successful.
        /// </param>
        /// <param name="resultType">
        /// A <see cref="CryptographicResultType"/> enumerated value indicating the type of 
        /// result.
        /// </param>
        /// <param name="resultData">
        /// The data resulting from the related operation.
        /// </param>
        public CryptographicResult(bool success, CryptographicResultType resultType, Stream resultData)
            : base()
        {
            _success = success;
            _resultData = resultData;
            _lastException = null;
            _resultType = resultType;
        }
        /// <summary>
        /// Initializes a new instance of the class to the specified values.
        /// </summary>
        /// <param name="success">
        /// A value indicating whether the operation was successful.
        /// </param>
        /// <param name="resultType">
        /// A <see cref="CryptographicResultType"/> enumerated value indicating the type of 
        /// result.
        /// </param>
        /// <param name="resultData">
        /// The data resulting from the related operation.
        /// </param>
        /// <param name="lastException">
        /// The last <see cref="Exception"/> instance that was encountered, or <b>null</b>.
        /// </param>
        public CryptographicResult(bool success, CryptographicResultType resultType, Stream resultData, Exception lastException)
            : base()
        {
            _success = success;
            _resultData = resultData;
            _lastException = lastException;
            _resultType = resultType;
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if ((!Disposed) && (disposing))
            {
            }
            _resultData = null;
            _lastException = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets or sets the type of result being represented.
        /// </summary>
        /// <value>
        /// A <see cref="CryptographicResultType"/> enumerated value
        /// indicating the result of the related operation.
        /// </value>
        public CryptographicResultType ResultType
        {
            get
            {
                return _resultType;
            }
            set
            {
                _resultType = value;
            }
        }
        /// <summary>
        /// Gets or sets a value indicating whether the operation was successful.
        /// </summary>
        /// <value>
        /// <b>true</b> if the operation succeeded; otherwise, <b>false</b>.
        /// </value>
        public bool Success
        {
            get
            {
                return _success;
            }
            set
            {
                _success = value;
            }
        }
        /// <summary>
        /// Gets or sets the reference to the <see cref="Stream"/> containing the 
        /// result of last operation, if successful.
        /// </summary>
        /// <value>
        /// A <see cref="Stream"/> instance or <b>null</b>.
        /// </value>
        public Stream ResultData
        {
            get
            {
                return _resultData;
            }
            set
            {
                _resultData = value;
            }
        }
        /// <summary>
        /// Gets or sets the reference to the last exception that was encountered.
        /// </summary>
        /// <value>
        /// An <see cref="Exception"/> instance or <b>null</b>.
        /// </value>
        public Exception LastException
        {
            get
            {
                return _lastException;
            }
            set
            {
                _lastException = value;
            }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Clears the contents of the object instance without disposing of the instance.
        /// </summary>
        public void Clear()
        {
            _lastException = null;
            _resultType = CryptographicResultType.None;
            _resultData = null;
            _success = false;
        }
        #endregion
    }
}
